/* 
Project: De-policing, Police Stops, and Crime
Published in Policing: A Journal of Policy and Practice

Author: Zachary A. Powell

Notes:

Data were analyzed in Stata/MP 15.1

Seed for bootstrap was 909 - the area code of where I work

Stops data from Stanford Policing Project will record some months as 0
Treated this as misisng as is recoded in the data set.

Crime data also had a few artificially low observations in vehicle/property 
crime.Suspect this has to do with not reporting certain crime categories in 
certain months. This observations are coded as missing. 

ORI = ORI code
ORID = encoded ORI; used to seet as panel ID
PanelID = used as main panelid
mdate = Month/date 
MonthYear = used to index time
Unemp = Unemployment rate from BLS at MSA level
Vehicle = Num. Vehicle Stops
Ped = Num. Pedestrian Stops
weirdPed = agencies with weird Ped counts (e.g., Baltimore
TStops = sum of Vehicle and Ped stops if agencies report both
PedRate = pedestrian stop rate per 100k
VehRate = vehicle stop rate per 100k
VehRd = rounded Vehicle Rate
PedRd = roudned Pedestrian Rate
TotRd = rounded Total Stops rate
DepScore = community disadvantage score
NoCov = flag if agency covers pre/post Ferguson effect
PctNonWht = percentage of non-white people in community
PctNonWhtL = logged percentage of non-white people in community
PropRd2 = rounded Property Crime rate. screwed up first iteration so there's a 2 
VioRd2 = rounded Violent Crime rate. same screw up with rounded property rrate
FergEff = abrupt/permanent ferguson effect
FergEff1-24 = numbers at end of FergEff indicate length of ferguson effect. 
values range from 1 month to 24 months
UnempL = logged unemployment rate
OffRate = Officer rate per 1k
OffRateL logged OffRate
PctNonWhtOff = percentage nonwhite officers; not used, collinear with pct. nonwht
PctNWOffL = logged nonwhite officers ; not used, collinear with pct. nonwht
PctWht = used for pct. non-wht. formula 100-PctWht
MedInc = median income
Poverty = pct. families in poverty
EduAttain = educational attainment (pct. 25+ w/ high school diploma)
SingP = Pct single parents with kids (male/female householder)
EduInv = inverse of educational attainment 100 - EduAttain. used so higher values 
indicate more disadvantage
VioCrime = Violent crime rate per 100k
PropCrime = Property crime rate per 100k
VioRateL = logged violent crime rate
PropRateL = logged property crime rate
PedRate = ped stop rate per 100k
VehRate = vehilce stop rate per 100k
PedRateL = logged ped stop rate
VehRateL = logged veh stop rate
TotRate = total stop rate
TotRateL = logged total stop rate

Code used to generate DepScore. Not run in this file.

Note: adding unemployment creates more factors which is less than desirable
also wanted to differentiate b/c unemp has monthly observations at MSA level. 
Other demographics are only measured annually and values are carried foward by group

gen EduInv = 100 - EduAttain

gen medinrl = log(MedInc) * - 1
egen medstd = std(medinrl)
egen povstd = std(Poverty)
egen edustd = std(EduInv)
egen singstd = std(SingP)
egen unempstd = std(Unemp)

factor medstd  povstd  edustd  singstd, pcf
rotate

gen DepScore = medstd + povstd + edustd + singstd

*/

/* recommend setting up a working directory to store log files.
bootstrapping takes a while so it is best to view at the end once everything is 
finished running.

also need to xtset after each model since bootstrapping randomly draws and mixes
up data

*/


* set working directory here

* cd

* graphs in paper


xtline VehRateL if NoCov < 1, tline(2014m8)
graph export VehRateLog.pdf, replace

xtline PedRateL if NoCov < 1 & Ped > 0 & weirdPed < 1, tline(2014m8)
graph export PedRateLog.pdf, replace

xtline TotRateL if NoCov < 1 & Ped > 0 & weirdPed < 1, tline(2014m8)
graph export TotRateLog.pdf, replace


xtline VioRateL if NoCov < 1 & weirdcrime < 1, tline(2014m8)
graph export VioRateLog.pdf, replace

xtline PropRateL if NoCov < 1 & weirdcrime < 1, tline(2014m8)
graph export PropRateLog.pdf, replace



log using FinalComm.log, replace
xtset PanelID MonthYear, monthly
xtnbreg Vehicle FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1, fe irr exposure(population) ///
vce(bootstrap ,seed(909) reps(5000) nodots)

xtset PanelID MonthYear, monthly
xtnbreg Ped FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, ///
fe irr exposure(population) vce(bootstrap ,seed(909) reps(5000) nodots)

xtset PanelID MonthYear, monthly
xtnbreg TStops FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL /// 
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, ///
fe irr exposure(population) vce(bootstrap ,seed(909) reps(5000) nodots)
log close

log using FinalComm100k.log, replace
xtset PanelID MonthYear, monthly
xtnbreg VehRd FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1, fe irr exposure(population) ///
vce(bootstrap ,seed(909) reps(5000) nodots)

xtset PanelID MonthYear, monthly
xtnbreg PedRd FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, ///
fe irr exposure(population) vce(bootstrap ,seed(909) reps(5000) nodots)

xtset PanelID MonthYear, monthly
xtnbreg TotRd FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL /// 
 i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, ///
fe irr exposure(population) vce(bootstrap ,seed(909) reps(5000) nodots)
log close



* both stop rates are stationary


log using FinalCrime.log, replace
xtset PanelID MonthYear, monthly
xtnbreg VioRd2 FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots)

xtset PanelID MonthYear, monthly
xtnbreg PropRd2 FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots)
log close



* Different Ferg Effs 

log using FergEffTestsStops.log, replace

*Vehicle Stops
xtset PanelID MonthYear, monthly
xtnbreg Vehicle FergEff1 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg Vehicle FergEff6 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg Vehicle FergEff12 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg Vehicle FergEff18 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg Vehicle FergEff24 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg Vehicle FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

* Pedestrian Stops
xtset PanelID MonthYear, monthly
xtnbreg Ped FergEff1 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg Ped FergEff6 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg Ped FergEff12 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg Ped FergEff18 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg Ped FergEff24 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg Ped FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

* Total Stops

xtset PanelID MonthYear, monthly
xtnbreg TStops FergEff1 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg TStops FergEff6 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg TStops FergEff12 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg TStops FergEff18 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg TStops FergEff24 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

xtset PanelID MonthYear, monthly
xtnbreg TStops FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & Ped > 0 & weirdPed < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) exposure(population)
estat ic

log close


log using FergEffTestsCrime.log, replace

* violent crime with different ferg effs
xtset PanelID MonthYear, monthly
xtnbreg VioRd2 FergEff1 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

xtset PanelID MonthYear, monthly
xtnbreg VioRd2 FergEff6 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

xtset PanelID MonthYear, monthly
xtnbreg VioRd2 FergEff12 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

xtset PanelID MonthYear, monthly
xtnbreg VioRd2 FergEff18 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

xtset PanelID MonthYear, monthly
xtnbreg VioRd2 FergEff24 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

xtset PanelID MonthYear, monthly
xtnbreg VioRd2 FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

*** Property crime
xtset PanelID MonthYear, monthly
xtnbreg PropRd2 FergEff1 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

xtset PanelID MonthYear, monthly
xtnbreg PropRd2 FergEff6 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

xtset PanelID MonthYear, monthly
xtnbreg PropRd2 FergEff12 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

xtset PanelID MonthYear, monthly
xtnbreg PropRd2 FergEff18 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

xtset PanelID MonthYear, monthly
xtnbreg PropRd2 FergEff24 d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

xtset PanelID MonthYear, monthly
xtnbreg PropRd2 FergEff d.UnempL d.OffRateL d.DepScore d.PctNonWhtL  ///
i.MonthYear if NoCov < 1 & weirdcrime < 1, fe irr vce(bootstrap, seed(909) ///
reps(5000) nodots) 
estat ic

log close







